package com.fs.myview;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.studentmanagersystem.AdminAccountUpdateActivity;
import com.example.studentmanagersystem.R;
import com.example.studentmanagersystem.URL;
import com.fs.adapters.ClassesAdapter;
import com.fs.po.UserData;
import com.fs.utils.CmdUtils;

public class AccountListView extends RelativeLayout implements View.OnClickListener {
	
	private View view;
	private RelativeLayout layout1,layout2;
	private TextView headIcon,nameTitle,moreButton,deleteBtn,addBtn,updateBtn;
	private Context context;
	public UserData userlistData;
	public String user;
	
	Handler handler;

	public AccountListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}

	public AccountListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public AccountListView(Context context) {
		super(context);
		init(context);
	}

	public void init(Context context) {

		this.context = context;
		
		this.view = ((Activity) context).getLayoutInflater().inflate(R.layout.userlist_tab, null);
		this.initWidget();

		
		this.addView(view);
	}

	
	private void initWidget() {
		this.createLayout1();
		this.createIcon();
		this.createNameTitle();
		this.createMoreButton();

		 
		this.createLayout2();
		this.createDelete();
		this.createAdd();
		this.createUpdate();

	}


	private void createUpdate() {
		updateBtn = (TextView) view.findViewById(R.id.userlist_tab_view_update);
		updateBtn.setOnClickListener(this);
	}

	private void createAdd() {
		addBtn = (TextView) view.findViewById(R.id.userlist_tab_view_add);
		addBtn.setOnClickListener(this);

	}

	
	private void createDelete() {
		deleteBtn = (TextView) view.findViewById(R.id.userlist_tab_view_delete);
		this.deleteBtn.setOnClickListener(this);
	}

	
	private void createLayout2() {

		layout2 = (RelativeLayout) view.findViewById(R.id.userlist_tab_view_layout_2);

	}

	
	private void createMoreButton() {

		moreButton = (TextView) view.findViewById(R.id.userlist_tab_view_more);
		moreButton.setOnClickListener(this);
	}

	

	
	private void createNameTitle() {
		nameTitle = (TextView) view.findViewById(R.id.userlist_tab_view_name);

	}

	private void createIcon() {
		 
		headIcon = (TextView) view.findViewById(R.id.userlist_tab_view_icon);

		 
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(100, 100);
		 
		lp.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
		headIcon.setLayoutParams(lp);

	}

	 
	private void createLayout1() {
		layout1 = (RelativeLayout) view.findViewById(R.id.userlist_tab_view_layout_1);

//		RelativeLayout.LayoutParams lpview = new RelativeLayout.LayoutParams(720, 240);
//
//		layout1.setLayoutParams(lpview);

	}

	//按钮图，背景图，增加图，修改图，删除图
	public void setImage(Drawable buttonDrawable, Drawable bgDrawable, Drawable addDrawable, Drawable updateDrawable, Drawable deleteDrawable) {
		 
		moreButton.setBackgroundDrawable(buttonDrawable);
		layout2.setBackgroundDrawable(bgDrawable);
		addBtn.setBackgroundDrawable(addDrawable);
		updateBtn.setBackgroundDrawable(updateDrawable);
		deleteBtn.setBackgroundDrawable(deleteDrawable);
	}

	public void setInfor(UserData userlistData,String user,Handler handler) {
		
	
		
		this.userlistData=userlistData;
		this.user=user;
		
		this.handler=handler;
		 
			headIcon.setBackgroundDrawable(userlistData.getIcon());
		 
		nameTitle.setText(userlistData.getName());

	}

	
	
	/**
	 * 事件监听，用户列表展开项的操作
	 */
	 
	@Override
	public void onClick(View v) {
		Intent intent;
		int id=v.getId();
		switch (id) {
			case R.id.userlist_tab_view_more:
				
					if (layout2.getVisibility() == layout2.VISIBLE) {
						layout2.setVisibility(layout2.GONE);
					} else {
						layout2.setVisibility(layout2.VISIBLE);
					}
					break;
			case R.id.userlist_tab_view_add:
				System.out.println("增");//【测】
				
				
				
				
					break;
					
					
					
			case R.id.userlist_tab_view_delete:
				System.out.println("删");//【测】
				//如果得到的数据不为空，就进行删除操作
				if((this.userlistData!=null)&&(user!=null)){
					
					showNormalDia();
					
				}
				
					break;
			case R.id.userlist_tab_view_update:
				
				
				Bundle bundle=new Bundle();
				
				bundle.putString("user_type", user);
				bundle.putInt("user_id", userlistData.getUser_id());
				intent=new Intent(context,AdminAccountUpdateActivity.class);
				
				intent.putExtras(bundle);
				context.startActivity(intent);
				
					break;
			default:
				break;
		}
	}

	
	
	/**
	   * 弹出提示对话框
	   */
	  private void showNormalDia(){  
	        
	        AlertDialog.Builder normalDia=new AlertDialog.Builder(context);  
	        normalDia.setIcon(R.drawable.ic_launcher);  
	        normalDia.setTitle("提示");  
	        normalDia.setMessage("确认删除？");  
	          
	        normalDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	            	
	            	
	            	
	            	String cmd="delete from "+user+" where _id="+userlistData.getUser_id()+"";
					
					CmdUtils cmdUtils=new CmdUtils(context);
					
					cmdUtils.submitCmd(URL.CMD_URL,"delete",cmd);
	            	
	                
					new Thread(){
						
						@Override
						public void run() {
							super.run();
							
							Message msg=Message.obtain();
							
							msg.what=0;
							msg.obj=user;
							
							handler.sendMessage(msg);
						}
						
					}.start();
	            }  
	        });  
	        
	        normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
	            @Override  
	            public void onClick(DialogInterface dialog, int which) {  
	               
	            }  
	        });  
	        normalDia.create().show();  
	    }  
	
	 
}
